<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { roomApi } from '../api/index'
import { Calendar, User, OfficeBuilding, Document } from '@element-plus/icons-vue'

const router = useRouter()
const rooms = ref([])
const loading = ref(false)
const user = ref(JSON.parse(localStorage.getItem('user')) || null)

onMounted(async () => {
  await fetchRooms()
})

const fetchRooms = async () => {
  loading.value = true
  try {
    const res = await roomApi.getAllRooms()
    rooms.value = res.data
  } catch (error) {
    console.error('获取会议室列表失败:', error)
    ElMessage.error('获取会议室列表失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

const goToReservation = () => {
  if (!user.value) {
    console.log('用户未登录，跳转到登录页')
    router.push('/login')
    return
  }
  console.log('跳转到预约页面')
  router.push('/new-reservation')
}
</script>

<template>
  <div class="home-container">
    <div class="welcome-card">
      <h1 class="welcome-title">欢迎使用会议室系统</h1>
      
      <div class="feature-cards">
        <div class="feature-card" @click="goToReservation()">
          <el-icon class="feature-icon"><Calendar /></el-icon>
          <div class="feature-title">计划预约</div>
          <div class="feature-desc">可提前7天预约会议室使用</div>
        </div>
        
        <div class="feature-card" @click="user ? router.push('/my-reservations') : router.push('/login')">
          <el-icon class="feature-icon"><User /></el-icon>
          <div class="feature-title">我的预约</div>
          <div class="feature-desc">查看您已预约的会议室</div>
        </div>
        
        <div class="feature-card" @click="user && user.role === 'ADMIN' ? router.push('/rooms') : router.push('/')">
          <el-icon class="feature-icon"><OfficeBuilding /></el-icon>
          <div class="feature-title">会议室概览</div>
          <div class="feature-desc">查看所有会议室的详情</div>
        </div>
        
        <div class="feature-card" @click="user ? router.push('/reservations') : router.push('/login')">
          <el-icon class="feature-icon"><Document /></el-icon>
          <div class="feature-title">预约管理</div>
          <div class="feature-desc">管理所有会议室的预约</div>
        </div>
      </div>
      
      <div class="action-button-container">
        <el-button type="primary" class="action-button" @click="goToReservation">
          立即预约会议室
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #f0f2f5;
}

.welcome-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 30px;
  width: 800px;
  max-width: 90%;
}

.welcome-title {
  text-align: center;
  font-size: 24px;
  color: #303133;
  margin-bottom: 40px;
}

.feature-cards {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 20px;
}

.feature-card {
  flex: 1;
  min-width: 150px;
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  font-size: 30px;
  color: #409EFF;
  margin-bottom: 15px;
}

.feature-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 10px;
}

.feature-desc {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

.action-button-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.action-button {
  padding: 12px 30px;
  font-size: 16px;
}

@media (max-width: 768px) {
  .feature-cards {
    flex-direction: column;
  }
  
  .feature-card {
    width: 100%;
  }
}
</style> 